package cn.edu.nuc.bre.controller;

import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.User;
import org.activiti.engine.identity.UserQuery;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import cn.edu.nuc.bre.service.interfaces.UserService;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * 后台用户管理
 * 
 * @author Mine
 * @since 2016年11月11日_上午8:39:58
 */
@Controller
@Scope("prototype") // 原型
public class UserAction extends ActionSupport {
	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private UserService userService;

	@Autowired
	private IdentityService identityService;

	private String tip;// 回显小消息

	// 页面传入参数封装
	private User user = new UserEntity();

	private PageInfo<User> pageInfo = null;
	// 分页信息
	private int targetPage;
	// 查询key
	private String keyword;

	// 获取用户列表
	@Override
	public String execute() throws Exception {
		// 设置查询条件
		UserQuery uq = identityService.createUserQuery();
		List<User> list = null;
		if (keyword != null && !"".equals(keyword.trim())) {
			uq = uq.userFirstNameLike("%" + keyword.trim() + "%");
		}
		long total = uq.count();// 获取总记录
		if (targetPage > 0) {
			list = uq.listPage((targetPage - 1) * 3, 3);// 默认页面大小
		} else {
			targetPage = 1;
			list = uq.listPage(0, 3);// 默认页面大小
		}
		pageInfo = new PageInfo<User>((int) total, targetPage, 3, list); // 获取pageInfo

		if (this.getTip() == null) {
			String temp = "";
			if (keyword != null && !"".equals(keyword.trim())) {
				temp = "关键字[" + this.getKeyword() + "]";
			}
			boolean flag = pageInfo.hasPageListErr();
			if (flag && this.getTip() == null) {
				tip = temp + "查询为空";
			} else {
				tip = temp + "查询成功";
			}
		}
		return SUCCESS;
	}

	/**
	 * 删除用户
	 */
	public String delete() throws Exception {
		String id = this.getUser().getId();
		if (userService.delete(id)) {
			this.setTip("删除[" + id + "]号用户成功!!!");
		} else {
			this.setTip("删除[" + id + "]号用户失败!!!");
		}
		return SUCCESS;
	}

	/**
	 * 获取修改用户信息页面
	 */
	public String showEdit() throws Exception {
		String id = this.getUser().getId();
		System.out.println(id);
		user = userService.findById(id);
		if (user != null) {
			this.setTip("获取" + id + "号用户信息成功。。。");
		} else {
			this.setTip("获取" + id + "号用户信息失败。。。");
		}
		// 不管成功失败都跳到修改页面
		return SUCCESS;
	}

	/**
	 * 修改用户信息
	 */
	public String edit() throws Exception {
		if (userService.edit(this.getUser())) {
			// 修改成功
			this.setTip("修改" + this.getUser().getId() + "号用户信息成功。。。");
			return SUCCESS;
		} else {
			// 修改失败
			this.setTip("修改" + this.getUser().getId() + "号用户信息失败。。。");
			return ERROR;
		}
	}

	/**
	 * 新增用户信息
	 */
	public String add() throws Exception {
		if (userService.add(this.getUser())) {
			// 添加成功
			this.setTip("添加" + this.getUser().getId() + "号用户信息成功。。。");
		} else {
			// 添加失败
			this.setTip("添加" + this.getUser().getId() + "号用户信息失败。。。");
		}
		// 回到展示页
		return SUCCESS;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public PageInfo<User> getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo<User> pageInfo) {
		this.pageInfo = pageInfo;
	}

	public int getTargetPage() {
		return targetPage;
	}

	public void setTargetPage(int targetPage) {
		this.targetPage = targetPage;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
}
